package com.pw.study.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @program: study_spring
 * @description: jdbc连接类
 * @author: future
 * @create: 2021-03-23 22:41
 **/

public class JDBCConnection {

    private static JDBCConnection connection =null;
    private Connection con=null;
    public static JDBCConnection getInstance() {
        if(connection==null){
            connection=new JDBCConnection();
        }
        return connection;
    }
    //定义一个方法返回connection对象
    public Connection getConnection(){
        if (con == null) {
            //1、装载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //2.定义连接url
            String url = "jdbc:mysql://localhost:3306/study?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false";
            //3.定义账号和密码
            String user = "root";
            String password = "123456";
            //4.获得连接对象
            try {
                con = DriverManager.getConnection(url, user, password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return con;

    }
}
